<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;

class BdeleteController extends Controller
{
    //  批量删除 批量删除分类
    public function Sortdelete()
    {
        // dd($request);
    	$data = DB::table('category')
             -> select('category.*', DB::raw("concat(category.path,',',category.id) as sortpath"))
             -> orderBy('sortpath')
             -> get();
        foreach ($data as $key => $value) {
            $num = substr_count($value -> path,',');
            $str = str_repeat('|----', $num);
            $data[$key] -> catname = $str.$value -> catname;
        }

        // dd($data);
        return view('admin.bdelete.sortdelete', ['title' => '批量删除分类', 'data' => $data,]);
    }

    // 处理批量选中的id
    public function update(Request $request)
    {
    	$data = $request -> except('_token','num','keywords');
        // dd($data);
        // 判断有没有子分类,如果有,就不删除
        $res = DB::table('category') -> whereIn('pid', $data['id']) -> first();
        if($res)
        {
            return back() -> with(['info' => '有子分类,不能够删除']);
        }
        // 如果没有分类 就给删除
        $res = DB::table('category') -> whereIn('id', $data['id']) -> delete();
        if($res)
        {
            return redirect('/Admin/category') -> with(['info' => '删除成功']);
        }else
        {
            return back() -> with(['info' => '删除失败']);
        }

    }

}
